package cn.e3.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.converter.ObjectToStringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.e3.utils.FastDFSClient;
import cn.e3.utils.JsonUtils;

@Controller
public class UploadPictureController {
	@Value("${address}")
	private String address;
	@RequestMapping("/pic/upload")
	@ResponseBody
	public String upload(MultipartFile uploadFile){
		Map map=new HashMap();
	try {
		//将图片上传到图片服务器
		FastDFSClient fastDFSClient=new FastDFSClient("classpath:fastdfs/client.conf");
		//上传图片
		String filename=uploadFile.getOriginalFilename();
		//获取文件的扩展名
		String extName=filename.substring(filename.lastIndexOf(".")+1) ;
		String fileaddress = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
		//将文件名补全
		fileaddress=address+fileaddress;
		
		map.put("error", 0);
		map.put("url", fileaddress);
		
	} catch (Exception e) {
		map.put("error", 1);
		map.put("url", "上传图片失败");
	}
	return JsonUtils.objectToJson(map);
	}
}
